package com.mingyuan.demo1;

import net.bytebuddy.implementation.bind.annotation.Origin;
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
import net.bytebuddy.implementation.bind.annotation.SuperCall;

import java.lang.reflect.Method;
import java.util.Random;
import java.util.concurrent.Callable;


public class MethodCostTime {

    @RuntimeType
    public static Object intercept(@Origin Method method, @SuperCall Callable<?> callable) throws Exception {
        long start = System.currentTimeMillis();
        try {
           //  随机休眠1-3秒
            Random rand = new Random();
            int randomNum = rand.nextInt((3000-1000 +1) + 1000);
            try {
                Thread.sleep(randomNum);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            // 原有函数执行
            return callable.call();
        } finally {
            System.out.println(method + " 方法耗时：" + (System.currentTimeMillis() - start) + "ms");
        }
    }

}
